#!/bin/bash -ex

wd=`pwd`

echo '
## Use SSH with CVS
export CVS_RSH=ssh

## Location of the self-compiled binary packages
export PKG_PATH=file:///var/tmp/packages/All

## Added for pkgsrc installed packages
export PATH="${PATH}:/usr/pkg/bin"
if [ `id -u` -eq 0 ]; then
	  export PATH="${PATH}:/usr/pkg/sbin"
fi
export MANPATH="${MANPATH}:/usr/pkg/man"
' > /tmp/profile_tmp

if ! `grep -q "PKG_PATH" /etc/profile` ; then
	cat /tmp/profile_tmp >> /etc/profile
fi

echo /usr/pkg/lib >> /etc/ld.so.conf
ldconfig

echo cvs -q >> /root/.cvsrc

wget http://kuparinen.org/martti/comp/slackware/pkgsrcupdate -O /usr/sbin/pkgsrcupdate
chmod 755 /usr/sbin/pkgsrcupdate

pkgsrcupdate -i || :
pkgsrcupdate

unset PKG_PATH
cd /usr/pkgsrc/bootstrap
sh bootstrap -h
sh bootstrap --workdir /tmp/workdir
sh cleanup
rm -rf /tmp/workdir

cd $wd

./mms -y || :

if ! `grep -q PKG_RCD_SCRIPTS /usr/pkg/etc/mk.conf 2>/dev/null` ; then

echo '
# Location of the startup scripts
PKG_RCD_SCRIPTS=YES
RCD_SCRIPTS_DIR=/usr/pkg/etc/rc.d

# Dont actually install the packages in /usr/pkg since we use binary packages
USE_DESTDIR=yes
DEPENDS_TARGET=package-install
UPDATE_TARGET=package-install

# Where to store distfiles and created binary packages
DISTDIR=/var/tmp/distfiles
PACKAGES=/var/tmp/packages

# Licenses
ACCEPTABLE_LICENSES+=vim-license

# Xorg
X11_TYPE=modular

# Per-package settings
#
# pkgsrc/mail/dovecot
PKG_OPTIONS.dovecot="inet6 pam"
#
# pkgsrc/mail/postfix
PKG_OPTIONS.postfix="pcre tls"
' >> /usr/pkg/etc/mk.conf

fi

mkdir -p /var/tmp/distfiles /var/tmp/packages

cd /usr/pkgsrc/pkgtools/rcorder
bmake package clean clean-depends
cd /var/tmp/packages/All
pkg_add rcorder

cd /usr/pkgsrc/pkgtools/lintpkgsrc
bmake package clean clean-depends
cd /var/tmp/packages/All
pkg_add lintpkgsrc

#cd /usr/pkgsrc/pkgtools/rc.subr
#bmake package clean clean-depends
#cd /var/tmp/packages/All
#pkg_add rc.subr

# Install CVS
cd /usr/pkgsrc/devel/scmcvs
bmake package clean clean-depends
cd /var/tmp/packages/All
pkg_add cvs

# Install SSH
cd /usr/pkgsrc/security/openssh
bmake package clean clean-depends
cd /var/tmp/packages/All
pkg_add openssh

# Remove Slackware packages needed during bootstrap
$wd/mms -by

echo "http://slackware.mirrors.tds.net/pub/slackware/slackware-13.0/" >> /etc/slackpkg/mirrors

slackpkg update
slackpkg upgrade-all

echo '
#!/bin/sh

# Update pkgsrc (give -c for HEAD instead of the latest stable branch)
/usr/sbin/pkgsrcupdate

# Check for vulnerabilities
/usr/pkg/sbin/download-vulnerability-list > /dev/null 2>&1
/usr/pkg/sbin/audit-packages

# Show packages which should be updated
/usr/pkg/bin/lintpkgsrc -i
' >> /etc/cron.daily/pkgsrc

chmod 755 /etc/cron.daily/pkgsrc

echo "


 It seems we are done here.


"

